package org.jeecg.modules.compilation.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.compilation.dto.*;
import org.jeecg.modules.compilation.entity.CiDoc;
import org.jeecg.modules.compilation.service.ICiDocService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.compilation.vo.AnnexFileVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ci_doc
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Api(tags="ci_doc")
@RestController
@RequestMapping("/compilation/ciDoc")
@Slf4j
public class CiDocController extends JeecgController<CiDoc, ICiDocService> {
	@Autowired
	private ICiDocService ciDocService;

	// ---------------------- 自定义接口 -----------------------


	 /**
	  * 首页查询
	  *
	  * @param document_title
	  * @return
	  */
	 @GetMapping(value="/searchlist")
	 @ApiOperation("ci_doc-查询业务文件")
	 public Result<List<SearchcIDocDraftDTO>> list(@RequestParam String document_title) throws IOException {
		 return Result.OK(ciDocService.searchList(document_title));
	 }
	 /**
	  * 根据business_category_sn 查询文件
	  * @param businessCategorySn
	  * @return
	  */
	@GetMapping(value="/getList")
	@ApiOperation("ci_doc-查询业务文件")
	public Result<List<CiDocDTO>> businessDocument(@RequestParam String businessCategorySn){
		return Result.OK(ciDocService.list(businessCategorySn));
	}
	 /*
	  * 全文检索
	  * */
	 @ApiOperation(value = "document全文检索", notes = "搜索")
	 @PostMapping("/search")
	 public Result<List<CiDoc>> search(@RequestBody SearchFileDTO searchFileDTO) throws IOException {
		 return Result.OK(ciDocService.search(searchFileDTO));
	 }

	 /**
	  * 根据文件编号展示文件内容
	  * @param documentSn
	  * @return
	  */
	 @GetMapping(value="/getDoc")
	 @ApiOperation("ci_doc-文件内容展示")
	 public Result<CiDocDTO> DocumentDisplay(@RequestParam String documentSn){

		 return Result.OK(ciDocService.getDoc(documentSn));

	 }
	 /**
	  * 预览接口
	  */
	 @ApiOperation(value="预览接口--preview文件夹", notes="预览接口")
	 @PostMapping(value = "/previewProcessing")
	 public Result<String>  PreviewProcessingInterface ( @RequestPart("file") MultipartFile preView ) throws Exception {
		 return Result.OK(ciDocService.previewProcessingInterface(preView));
	 }
	 /**
	  * save 文件+附件+json文件信息
	  */
	 @ApiOperation(value="添加", notes="save 文件+附件+json文件信息")
	 @PostMapping(value = "/save")
	 public Result<String> save(
			 @RequestPart("dto") CiDocEditOrUpdateDTO dto,
			 @RequestPart("file") MultipartFile  docFile ,
			 @RequestPart(value = "annex",required = false) MultipartFile [] annexFile
	 ) throws Exception {
		 if(ciDocService.save(dto, docFile, annexFile))
			 return Result.OK("添加成功！");
		 else
			 return Result.error("添加失败！");
	 }
	 /**
	  * doc文件下载
	  */
	 @ApiOperation(value="doc文件预览--CI文件夹", notes="doc文件预览")
	 @GetMapping(value = "/previewCi")
	 public Result<String> previewCi(@RequestParam String documentSn) throws Exception {
		 Result result = new Result();
		 result.setResult(ciDocService.previewCi(documentSn));
		 return result;
	 }
	// ---------------------- 自定义接口 -----------------------

	 /**
	  * 分页列表查询
	  *
	  * @param ciDoc
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "ci_doc-分页列表查询")
	 @ApiOperation(value="ci_doc-分页列表查询", notes="ci_doc-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<IPage<CiDoc>> queryPageList(CiDoc ciDoc,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											   HttpServletRequest req) {
		 QueryWrapper<CiDoc> queryWrapper = QueryGenerator.initQueryWrapper(ciDoc, req.getParameterMap());
		 Page<CiDoc> page = new Page<CiDoc>(pageNo, pageSize);
		 IPage<CiDoc> pageList = ciDocService.page(page, queryWrapper);
		 return Result.OK(pageList);

	 }

	 /**
	  *   添加
	  *
	  * @param ciDoc
	  * @return
	  */
	 @AutoLog(value = "ci_doc-添加")
	 @ApiOperation(value="ci_doc-添加", notes="ci_doc-添加")
//	@RequiresPermissions("compilation:ci_doc:add")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody CiDoc ciDoc) throws IOException {
		 ciDocService.save(ciDoc);
		 ciDocService.insertById(Long.valueOf(ciDoc.getId()));
		 return Result.OK("添加成功！");
	 }

	 /**
	  * 编辑文件信息
	  */
	 @ApiOperation(value="编辑文件信息", notes="编辑文件信息")
	 @PostMapping(value = "/edit")
	 public Result<String> edit(
			 @RequestPart("dto") CiDocEditOrUpdateTempDTO dto,
			 @RequestPart(value = "file",required = false) MultipartFile  docFile ,
			 @RequestPart(value = "annex",required = false) MultipartFile []  annexFiles ) throws Exception {

		 log.info("附件信息：{}",dto.getStrAnnexFiles());
		 log.info("附件数据转换：{}", JSON.parseArray(dto.getStrAnnexFiles(), AnnexFileVO.class));

		 CiDocEditOrUpdateDTO editOrUpdateDTO = new CiDocEditOrUpdateDTO();
		 BeanUtils.copyProperties(dto,editOrUpdateDTO);
		 editOrUpdateDTO.setAnnexFiles(JSON.parseArray(dto.getStrAnnexFiles(), AnnexFileVO.class));
		 if(ciDocService.edit(editOrUpdateDTO , docFile ,annexFiles ))
			 return Result.OK("编辑成功！");
		 else
			 return Result.error("编辑失败！");
	 }
	/**
	 *删除文件
	 */
	@AutoLog(value = "删除文件")
	@ApiOperation(value="删除文件", notes="删除文件")
	@GetMapping(value = "/removeBySn")
	public Result<String > remove(@RequestParam String  documentSn)   {
		if( ciDocService.removeBySn(documentSn) )
			return Result.OK("删除成功!");
		else
			return Result.error("删除失败!");
	}
	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "ci_doc-通过id删除")
	 @ApiOperation(value="ci_doc-通过id删除", notes="ci_doc-通过id删除")
//	@RequiresPermissions("compilation:ci_doc:delete")
	 @DeleteMapping(value = "/delete")
	 public Result<String> delete(@RequestParam(name="id",required=true) String id) throws IOException {
		 ciDocService.removeById(id);
		 ciDocService.deleteById(Long.valueOf(id));
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "ci_doc-批量删除")
	 @ApiOperation(value="ci_doc-批量删除", notes="ci_doc-批量删除")
//	@RequiresPermissions("compilation:ci_doc:deleteBatch")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		 this.ciDocService.removeByIds(Arrays.asList(ids.split(",")));
		 ciDocService.deleteBatch(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功!");
	 }
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "ci_doc-通过id查询")
	@ApiOperation(value="ci_doc-通过id查询", notes="ci_doc-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CiDoc> queryById(@RequestParam(name="id",required=true) String id) {
		CiDoc ciDoc = ciDocService.getById(id);
		if(ciDoc==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ciDoc);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ciDoc
    */
//    @RequiresPermissions("compilation:ci_doc:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CiDoc ciDoc) {
        return super.exportXls(request, ciDoc, CiDoc.class, "ci_doc");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("compilation:ci_doc:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CiDoc.class);
    }

}
